package com.nttuan.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nttuan.flows.ContactListener;
import com.nttuan.model.entity.Contact;
import com.nttuan.model.entity.User;
import com.nttuan.mycalender.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by NT Tuan on 10/29/2014.
 */
public class ContactDetailAdapter  extends BaseAdapter{
    private Context context;
    private List<Contact> list;
    private LayoutInflater inflater;
    private ContactListener listener;
    private User me;
    public ContactDetailAdapter(Context context, List<Contact> list, User me){

        this.list = list;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.me = me;
    }

    public void setListener(ContactListener listener) {
        this.listener = listener;
    }

    @Override
    public int getCount() {
        if(list == null)
            return 0;
        return list.size();
    }

    public void setList(List<Contact> list) {
        this.list = list;
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHoller holler;
        if(view == null){
            holler = new ViewHoller();
            view = inflater.inflate(R.layout.item_contact2, null);
            holler.userName =(TextView)view.findViewById(R.id.textNameItemContactDetail);
            holler.layout =(RelativeLayout)view.findViewById(R.id.lLayoutItemContactDetail);
            holler.remove =(ImageView)view.findViewById(R.id.imgRemoveItemContactDetail);
            view.setTag(holler);
        }else
            holler =(ViewHoller)view.getTag();
        final Contact contact = list.get(i);
        if(contact.isAttend())
            holler.layout.setBackgroundResource(R.drawable.coming_primary_bg);
        else
            holler.layout.setBackgroundResource(R.drawable.coming_second_bg);
        if(contact.getUser().getEmail().equals(me.getEmail()))
            holler.userName.setText(R.string.me);
        else {
            String title = contact.getUser().getUserName();
            if(title != null && !title.equals(""))
                holler.userName.setText(title);
            else
                holler.userName.setText(contact.getUser().getEmail());

        }
        holler.remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(listener != null)
                    listener.remove(contact, i);
            }
        });
        return view;
    }
    class ViewHoller{
        ImageView remove;
        TextView userName;
        RelativeLayout layout;
    }
}
